package dao

import (
	"context"
	"database/sql/driver"
	"sale-admin/internal/dal"
	"sale-admin/internal/models"
	"time"

	"github.com/golang-module/carbon/v2"
	"gorm.io/gorm"
)

type RobotInventory struct{}

type DriverValuer time.Time

// Value 实现了 driver.Valuer 接口
func (v DriverValuer) Value() (driver.Value, error) {
	return time.Time(v), nil
}

func (RobotInventory) GetRobotInventoryList(ctx context.Context, db *gorm.DB, robotName, date string) (data []*models.MaRobotInventory, count int64, err error) {
	m := dal.Use(db).MaRobotInventory
	tx := m.WithContext(ctx)
	if robotName != "" {
		tx = tx.Where(m.RobotName.Eq(robotName))
	}
	if date != "" {
		d := carbon.Parse(date).ToStdTime()
		tx = tx.Where(m.Date.Eq(DriverValuer(d))) // 解决time.Time转换为driver.Valuer的问题
	}
	data, err = tx.Find()
	return data, int64(len(data)), err
}
